import 'package:flutter/material.dart';
import 'package:myapp/widget/PPBaseWidget.dart';

class PPButtonView extends StatefulWidget with PPBaseWidget {

    PPButtonView({
        Key key, 
        this.btnPadding, 
        this.btnMargin, 
        this.btnWidth,
        this.btnHeight,
        this.btnColor,
        this.btnDecoration,
        @required
        this.btnChild,
        @required
        this.onPressedAction,
    }): super(key: key);

    final EdgeInsets btnPadding;
    final EdgeInsets btnMargin;
    final double btnWidth;
    final double btnHeight;
    final Widget btnChild;
    final Color btnColor;
    final BoxDecoration btnDecoration;
    final Function onPressedAction;

    _PPButtonViewState createState() => _PPButtonViewState();
}

class _PPButtonViewState extends State<PPButtonView> {

    EdgeInsets btnPadding;
    EdgeInsets btnMargin;
    double btnWidth;
    double btnHeight;
    Color btnColor;
    BoxDecoration btnDecoration;
    Widget btnChild;

    @override
    void initState() {
        super.initState();
        btnPadding = widget.btnPadding != null ? widget.btnPadding : EdgeInsets.all(0);
        btnMargin = widget.btnMargin != null ? widget.btnMargin : EdgeInsets.all(0);
        btnWidth = widget.btnWidth;
        btnHeight = widget.btnHeight;
        btnDecoration = widget.btnDecoration;
        btnChild = widget.btnChild;
        btnColor = widget.btnColor != null ? widget.btnColor : Colors.transparent;
    }

    @override
    void didUpdateWidget(PPButtonView oldWidget) {
        super.didUpdateWidget(oldWidget);
        btnPadding = widget.btnPadding != null ? widget.btnPadding : EdgeInsets.all(0);
        btnMargin = widget.btnMargin != null ? widget.btnMargin : EdgeInsets.all(0);
        btnWidth = widget.btnWidth;
        btnHeight = widget.btnHeight;
        btnDecoration = widget.btnDecoration;
        btnChild = widget.btnChild;
    }

    @override
    Widget build(BuildContext context) {
        return Container(
            width: btnWidth,
            height: btnHeight,
            color: btnDecoration == null ? btnColor : null,
            margin: btnMargin,
            padding: EdgeInsets.all(0),
            decoration: btnDecoration,
            child: FlatButton(
                splashColor: Colors.transparent,
                focusColor: Colors.transparent,
                highlightColor: Colors.transparent,
                padding: btnPadding,
                child: btnChild,
                onPressed: _onPressedAction,
            ),
        );
    }

    _onPressedAction() {
        if(widget.onPressedAction != null) {
            widget.onPressedAction();
        }
    }
}